package com.guchenbo.spark.sql

import org.apache.spark.sql.{SparkSession, SparkSessionExtensions}
import org.apache.spark.sql.catalyst.parser.ParserInterface
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan

/**
 * @author guchenbo
 * @date 2022/3/8
 */
object SqlParserDemo {

  def main(args: Array[String]): Unit = {
    type ParserBuilder = (SparkSession, ParserInterface) => ParserInterface
    type ExtensionsBuilder = SparkSessionExtensions => Unit
    val parserBuilder: ParserBuilder = (_, parser) => new StrictParser(parser)
    val extBuilder: ExtensionsBuilder = { e => e.injectParser(parserBuilder)}
    val spark =  SparkSession
      .builder()
      .appName("Spark SQL basic example")
      .config("spark.master", "local[*]")
      .withExtensions(extBuilder)
      .getOrCreate()

    val    sql = "select  * from table"


    val logicalPlan = spark.sessionState.sqlParser.parsePlan(sql)
    println(logicalPlan)

    spark.sparkContext.setLogLevel("ERROR")

    spark.sql("create * FROM turing_monitor.gp_public_model_report_psi_test_df LIMIT 1").show()

  }
}
